'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2019-10-03 10:51:17
@LastEditTime: 2019-10-03 22:37:50
'''
import os
import numpy as np

class Point(object):
    def __init__(self, path):
        self.path = path
        # self.contf = 0
        self.overlapf = []
        self.gap = []
        if self.path[-1] != '/':
            self.path += '/'
        

    def read(self, filename):
        with open(filename) as infile:
            while True:
                line = infile.readline()
                if line == '':
                    break
                line = line.split('\t')
                if line[0] == 'overlapf=':
                    self.overlapf.append(float(line[1]))
                    # self.contf += 1
                if line[0] == 'gap=':
                    self.gap.append(float(line[1]))


    def get_overlapf(self):
        tmp = np.array(self.overlapf)
        return tmp.mean()


    def get_gap(self):
        tmp = np.array(self.gap)
        return tmp.min()

    def get_rand_overlapf(self):
        re = []
        tmp = np.array(self.overlapf)
        for i in range(1, len(self.overlapf)):
            re.append(tmp[0:i].mean())
        return re

    def read_all(self):
        for filename in os.listdir(self.path):
            if filename[:3] == 'out':
                filename = self.path + filename
                # print(filename)
                self.read(filename)